/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import entities.Town;
import facades.TownFacade;
import javax.faces.bean.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

/**
 *
 * @author damfercer
 */

@Named
@ApplicationScoped
public class TownConverter implements Converter{
    
    /*@Inject
    @ResourceList
    Map<Integer,Town> townsMap;*/
    @Inject
    private TownFacade townFacade;
    
    @Override
    public Town getAsObject(FacesContext context, UIComponent component, String value) {
        Town town=null;
        try {
            Integer i=new Integer(value);
            //town=townsMap.get(i);
            town=townFacade.find(i);
        } catch (NumberFormatException e) {
        }
        return town;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String res=null;
        Town town=null;
        if(value.getClass().equals(Town.class)){
            town=(Town)value;
        }
        if(town!=null){
            res=town.getIdTown().toString();
        }
        return res;
    }
    
}
